<?php
namespace app\before\controller;

class AdminBill extends Base
{
    private $tn = 'admin_bill';

    public function index()
    {
        if (isset($this->get['page']) && isset($this->get['limit'])) {
            $where = [];
            //返利方式
            if(isset($this->get['type'])){
                $where['type'] = $this->get['type'];
            }
            //指定区域代理id
            if(session('admin_id') == 1){
                if(isset($this->get['admin_id'])){
                    $where['admin_id'] = $this->get['admin_id'];
                }
            }else{
                $where['admin_id'] = session('admin_id');
            }
           
           

            //返利时间
            if(isset($this->get['date'])){
                $where['b.add_time'] = [['egt',strtotime($this->get['date'])],['lt',strtotime($this->get['date'].'+1 day')]];
            }
            $list  = db($this->tn)->alias('b')->join(['admin_admin' => 'a'],'a.id = b.admin_id','left')->field('b.*,a.username')->where($where)->order('id', 'desc')->page($this->get['page'])->limit($this->get['limit'])->select();
            foreach ($list as $k => $v) {
                $list[$k]['add_time'] = date('Y-m-d H:i',$v['add_time']);
            }
            $count = db($this->tn)->alias('b')->join(['admin_admin' => 'a'],'a.id = b.admin_id','left')->where($where)->count();
            $this->returnAPI('', 0, $list, ['count' => $count, 'limit' => $this->get['limit']]);
        }
        return $this->fetch();
    }

    public function edit()
    {
        $model = model($this->tn);
        if ($this->post) {
            if ($id) {
                $model = $model->get($id);
                if (is_null($model)) {
                    $this->returnAPI('信息有误,请稍后重试');
                }
            }
            $result = $model->validate($this->tn)->allowField(true)->save($this->post);
            if ($result !== false) {
                $this->returnAPI('操作成功', 0);
            }
            $this->returnAPI($model->getError());
        }
        $model = $model->get($id);
        if (is_null($model)) {
            $data = $this->table_fields($this->tn);
        } else {
            $data = $model->getData();
        }
        return $this->fetch('', ['data' => $data]);
    }


}
